package com.jic.order.impl;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.api.rest.MememberApi;
import com.jic.member.response.MememberUserResponse;
import com.jic.order.entity.Order;
import com.jic.order.entity.OrderParent;
import com.jic.order.manage.request.OrderDistributionMemberRequest;
import com.jic.order.manage.request.OrderSaleReportRequest;
import com.jic.order.manage.response.OrderDistributionMembertResponse;
import com.jic.order.manage.response.OrderSaleReportResponse;
import com.jic.order.mapper.OrderMapper;
import com.jic.order.mapper.OrderParentMapper;
import com.jic.order.response.OrderAllAmountResponse;
import com.jic.order.service.OrderParentService;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单父表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-01-15 07:06:30
 */
@Slf4j
@Service
public class OrderParentServiceImpl implements OrderParentService {

	@Autowired
	OrderParentMapper orderParentMapper;

	@Autowired
	OrderMapper orderMapper;

	@Resource
	MememberApi mememberApi;

	@Override
	public RestResult<Boolean> updateParentOrderStatus(String orderNo) {
		Order order = orderMapper.queryByOrderNo(orderNo);
		if (order.getParentOrderNo().isEmpty()) {
			log.error("订单号为空");
			return RestResult.error("9999", "订单号为空");
		}
		OrderParent orderParent = new OrderParent();
		orderParent.setParentOrderNo(order.getParentOrderNo());
		orderParent.setOrderStatus(10);
		long flag = orderParentMapper.update(orderParent);

		order.setOrderStatus(10);
		orderMapper.update(order);
		return RestResult.success(flag > 0);
	}

	@Override
	public RestResult<List<OrderAllAmountResponse>> queryAllAmount(List<Long> memberId) {
		return RestResult.success(orderParentMapper.queryAllAmount(memberId));
	}

	@Override
	public RestResult<PageResult<OrderSaleReportResponse>> querySaleOrderReportPage(OrderSaleReportRequest orderSaleReportRequest) {
		int startIndex = (orderSaleReportRequest.getStart() - 1) * orderSaleReportRequest.getLimit();
		List<OrderSaleReportResponse> result = orderParentMapper.querySaleOrderReportPage(startIndex, orderSaleReportRequest);
		//        result.forEach(item->{
		//            RestResult<MememberUserResponse> memberUserInfo = memberFeignApi.getMemberUserInfo(item.getBuyerId());
		//            if(memberUserInfo!=null && memberUserInfo.getData()!=null){
		//                item.setBuyerUsername(memberUserInfo.getData().getNickName());
		//                item.setUserAccount(memberUserInfo.getData().getUserAccount());
		//            }
		//        });
		long count = orderParentMapper.querySaleOrderReportPageCount(orderSaleReportRequest);
		PageResult<OrderSaleReportResponse> pageResult = new PageResult<>();
		pageResult.setRows(result);
		pageResult.setTotal(count);
		return RestResult.success(pageResult);
	}

	@Override
	public List<OrderParent> queryOrderInfoByBuyId(OrderParent orderParent) {
		return orderParentMapper.queryOrderInfoByBuyId(orderParent);
	}

	@Override
	public RestResult<PageResult<OrderDistributionMembertResponse>> queryOrderDistributionListPage(OrderDistributionMemberRequest request) {
		int startIndex = (request.getStart() - 1) * request.getLimit();
		request.setBuyerType(1);
		request.setOrderStatus(10);
		request.setDeleteFlag(0);
		List<OrderDistributionMembertResponse> result = orderParentMapper.queryOrderDistributionListPage(startIndex, request);
		result.forEach(item -> {
			RestResult<MememberUserResponse> mememberUserResponseRestResult = mememberApi.queryMemberUserInfo(item.getDistrbutionId());
			if (null != mememberUserResponseRestResult && null != mememberUserResponseRestResult.getData()) {
				item.setNickName(mememberUserResponseRestResult.getData().getNickName());
				item.setUserAccount(mememberUserResponseRestResult.getData().getUserAccount());
				item.setPhone(mememberUserResponseRestResult.getData().getPhone());
				item.setCreateTime(mememberUserResponseRestResult.getData().getRegistrationTime());
			}
		});
		long count = orderParentMapper.queryOrderDistributionListPageCount(request);
		PageResult<OrderDistributionMembertResponse> pageResult = new PageResult<>();
		pageResult.setRows(result);
		pageResult.setTotal(count);
		return RestResult.success(pageResult);
	}

	@Override
	public RestResult<PageResult<OrderDistributionMembertResponse>> queryOrderDistributionInfoList(OrderDistributionMemberRequest request) {
		int startIndex = (request.getStart() - 1) * request.getLimit();
		request.setBuyerType(1);
		request.setOrderStatus(10);
		request.setDeleteFlag(0);
		List<OrderDistributionMembertResponse> result = orderParentMapper.queryOrderDistributionInfoList(startIndex, request);
		result.forEach(item -> {
			RestResult<MememberUserResponse> mememberUserResponseRestResult = mememberApi.queryMemberUserInfo(item.getBuyerId());
			if (null != mememberUserResponseRestResult && null != mememberUserResponseRestResult.getData()) {
				item.setNickName(mememberUserResponseRestResult.getData().getNickName());
				item.setUserAccount(mememberUserResponseRestResult.getData().getUserAccount());
				item.setPhone(mememberUserResponseRestResult.getData().getPhone());
			}
		});
		long count = orderParentMapper.queryOrderDistributionInfoListCount(request);
		PageResult<OrderDistributionMembertResponse> pageResult = new PageResult<>();
		pageResult.setRows(result);
		pageResult.setTotal(count);
		return RestResult.success(pageResult);
	}

	@Override
	public JSONObject queryOrderParentByOrderNo(String parentOrderNo) {
		return orderParentMapper.queryOrderParentByOrderNo(parentOrderNo);
	}

	@Override
	public long insert(OrderParent orderParent) {
		return orderParentMapper.insert(orderParent);
	}

	@Override
	public long delete(Long id) {
		return orderParentMapper.delete(id);
	}

	@Override
	public long update(OrderParent orderParent) {
		return orderParentMapper.update(orderParent);
	}

	@Override
	public List<JSONObject> querySettList(JSONObject jsonObject) {
		return orderParentMapper.querySettList(jsonObject);
	}


	@Override
	public Long queryGoodByOrderNo(String orderParentNo) {
		return orderParentMapper.queryGoodByOrderNo(orderParentNo);
	}

	@Override
	public Integer updateSettlementStatus(JSONObject param) {
		return orderParentMapper.updateSettlementStatus(param);
	}


}
